#include "gaia_common.h"
#include "cJSON.h"

/************************************************************************
 * 函数名：gaia_parse_post_data
 * 函数功能：解析uri的json
 * 参数说明：flags:  0 标识添加主机, 1 标识获取主机状态信息
 *                   2 标识状态查询(只有task_id)
 * 函数返回值：成功　0,失败　1
 ************************************************************************/
int gaia_parse_post_data(int flags,const char *uri,struct hostinfo **host_info)
{
  cJSON *root;
  int value_int;
  char *value_string = NULL;
  printf("%s\n",uri);
  /*
  memset((*host_info)->username,'\0',sizeof((*host_info)->username));
  */
  memset( (*host_info)->sign,'\0',sizeof( (*host_info)->sign));
  root = cJSON_Parse(uri);
  if(flags == 0)
  {
    strcpy( (*host_info)->username,cJSON_GetObjectItem(root,"username")->valuestring);
    (*host_info)->port = cJSON_GetObjectItem(root,"port")->valueint;  //获取port
    strcpy( (*host_info)->passwd,cJSON_GetObjectItem(root,"passwd")->valuestring);
    strcpy( (*host_info)->sign,cJSON_GetObjectItem(root,"sign")->valuestring);
    strcpy( (*host_info)->host,cJSON_GetObjectItem(root,"host")->valuestring);
    strcpy( (*host_info)->deploy_type,cJSON_GetObjectItem(root,"deploy_type")->valuestring);
    (*host_info)->hostid = 0;
  }else if(flags == 1)
  {
    //如果不存在的参数中，不包含的结构体成员，将以字符串“NULL”填充
    //strcpy( (*host_info)->username,"NULL");
    //(*host_info)->port = 0;
    (*host_info)->hostid = cJSON_GetObjectItem(root,"hostid")->valueint;
    //strcpy( (*host_info)->passwd,"NULL");
    strcpy( (*host_info)->sign,cJSON_GetObjectItem(root,"sign")->valuestring);
    //strcpy( (*host_info)->host,"NULL");
    strcpy( (*host_info)->task,cJSON_GetObjectItem(root,"task")->valuestring)  ;
  }else if(flags == 2){
     (*host_info)->taskid = cJSON_GetObjectItem(root,"task_id")->valueint;
  }
   
   //printf("sign = %s\n",(*host_info)->sign);
   //printf("strlen(sign) = %lu\n",sizeof((*host_info)->sign) );

  return 0;
}
